package com.wms.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.annotation.MyLog;
import com.wms.entity.OrderPlatform;
import com.wms.entity.OrderReturn;
import com.wms.entity.OrderReturnDTO;
import com.wms.listener.OrderPlatformListener;
import com.wms.listener.OrderReturnListener;
import com.wms.service.OrderReturnService;
import com.wms.util.AliyunOSSUtil;
import com.wms.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * ---Do First---
 *
 * @author:wellseasun
 * @date:2025/4/29 下午 8:11
 * @desc:
 */

@RestController
@RequestMapping("/order/return")
public class OrderReturnController {
    @Autowired
    private OrderReturnService orderReturnService;

    @GetMapping("/list")
    @MyLog(value = "按条件查询退货订单", type = "查询")
    public R findAllByPage(OrderReturnDTO orderReturnDTO){
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(orderReturnDTO.getPageNum(),orderReturnDTO.getPageSize());
        List<OrderReturn> list = orderReturnService.findAllByPage(orderReturnDTO);
        PageInfo<OrderReturn> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    /**
     * 新增退货订单
     */
    @PostMapping("/add")
    @MyLog(value = "按条件新增采购订单", type = "新增")
    public R add(@RequestBody OrderReturn orderReturn){
        Boolean flag = orderReturnService.add(orderReturn);
        if(flag){
            return R.ok();
        }else{
            return R.fail();
        }
    }
    /**
     * 批量删除
     */
    @PostMapping("/delete")
    @MyLog(value = "按条件删除退货订单", type = "修改")
    public R delete(@RequestParam List<Integer> ids){
        Boolean flag = orderReturnService.deleteOrderPurchase(ids);
        if(flag){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @MyLog(value = "按条件修改退货订单", type = "修改")
    public R update(@RequestBody OrderReturn orderReturn){
        Boolean flag = orderReturnService.updateOrderPurchase(orderReturn);
        if(flag){
            return R.ok();
        }else{
            return R.fail();
        }
    }
    /**
     * 导入
     */
    @PostMapping("/upload/excel")
    @MyLog(value = "导入退货订单", type = "导入")
    public R uploadExcel(MultipartFile file){
        try {
            EasyExcel.read(file.getInputStream(), OrderReturn.class, new OrderReturnListener(orderReturnService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
    /**
     * 导出
     */
    @GetMapping("/download/excel")
    @MyLog(value = "按条件导出退货订单", type = "导出")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        //  查询数据库全部数据
        List<OrderReturn> list = orderReturnService.findAllByPage(null);

        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("退货订单信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), OrderReturn.class).autoCloseStream(Boolean.FALSE).sheet("退货订单信息")
                .doWrite(list);
    }
}
